<template>
  <div class="real-name-page">
    <div class="breadcrumb">
      <Breadcrumb :data="breadcrumbData"></Breadcrumb>
    </div>
    <main>
      <div class="table-container">
        <div class="tabs">
          <ul class="list">
            <li
              :class="tabType === '0' ? 'active' : ''"
              @click="handleTabClick('0')">
              劳务人员
            </li>
            <li
              :class="tabType === '1' ? 'active' : ''"
              @click="handleTabClick('1')">
              管理人员
            </li>
            <li
              :class="tabType === '2' ? 'active' : ''"
              @click="handleTabClick('2')">
              特种人员
            </li>
            <li
              :class="tabType === '3' ? 'active' : ''"
              @click="handleTabClick('3')">
              五大员备案
            </li>
          </ul>
          <div class="search-container">
            <div class="search-content">
              <div class="search-item">
                <label>关键字：</label>
                <el-input
                  placeholder="姓名/身份证号"
                  v-model="searchValue"
                  @keydown.native.enter="handleSearch"
                ></el-input>
              </div>
              <el-button type="primary" @click="handleSearch">
                <i class="el-icon-search"></i>
              </el-button>
              <el-button type="primary" @click="handleRefresh">
                <i class="el-icon-refresh"></i>
              </el-button>
            </div>
          </div>
          <div class="btns">
<!--            <el-button type="primary">打印二维码</el-button>-->
            <el-button v-if="hasTabAdd" type="primary" @click="handleAdd">新建</el-button>
          </div>
        </div>
        <el-table
          class="table-content"
          :data="tableData"
          v-loading="tableLoading"
          element-loading-custom-class="custom-load"
          style="width: 100%">
          <el-table-column label="人员信息">
            <template slot-scope="scope">
              <div class="info">
                <img :src="scope.row.headPhoto" alt="">
                <div class="detail">
                  <p class="name">{{ scope.row.name }}</p>
                  <p>
                    <span v-if="scope.row.gender">{{ scope.row.gender }}</span>
                    <span>{{ setAge(scope.row.birthday) }}岁</span>
                    <span>{{ scope.row.nation }}</span>
                    <span>{{ scope.row.birthPlace }}</span>
                  </p>
                  <p>{{ scope.row.idNumber }}</p>
                </div>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="详细信息">
            <template slot-scope="scope">
              <div class="detail-info">
                <p><span>班组：</span>{{ scope.row.role }}</p>
                <p><span>工种：</span>{{ scope.row.job }}</p>
                <p><span>联系方式：</span>{{ scope.row.phone }}</p>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="进出场日期">
            <template slot-scope="scope">
              <div class="detail-info">
                <p><span>进场：</span>{{ scope.row.enterTime }}</p>
                <p><span>离场：</span>{{ scope.row.leaveTime || '--' }}</p>
              </div>
            </template>
          </el-table-column>
          <el-table-column prop="status" label="状态"></el-table-column>
          <el-table-column label="操作" width="200">
            <template slot-scope="scope">
              <i class="iconfont iconchakan" @click="handleLook(scope.row)"></i>
            </template>
          </el-table-column>
          <div class="empty-content" slot="empty">
            <i class="iconfont iconwushuju"></i>
            <p>暂无数据</p>
          </div>
        </el-table>
        <el-pagination
          v-if="total > 10"
          :page-size="pageSize"
          :current-page="pageNo"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>

      <!--新增/编辑人员信息弹框-->
      <personnel-info-model
        :visible="visible"
        :formData="formData"
        :type="type"
        @cancel="visible = false">
      </personnel-info-model>
    </main>
  </div>
</template>

<script>
import Breadcrumb from '@/components/public/Breadcrumb';
import personnelInfoModel from '@/components/project/personnel/personnelInfoModel';

export default {
  data() {
    return {
      // 面包屑数据
      breadcrumbData: [
        {
          name: '人员管理',
        },
        {
          name: '人员实名制'
        }
      ],
      // tab类型
      tabType: '0',
      // 人员新增、编辑类型
      type: 'add',
      // 关键字搜索值
      searchValue: '',
      // 是否为搜索状态
      hasSearch: false,
      // 列表数据
      tableData: [],
      pageNo: 1,
      pageSize: 10,
      total: 0,
      tableLoading: false,
      visible: false,
      // 人员信息数据
      formData: {}
    };
  },
  computed: {
    // 项目id
    projectId() {
      return this.$store.state.currentProjectId;
    }
  },
  created() {
    this.getTableData();
  },
  components: {
    Breadcrumb,
    personnelInfoModel
  },
  methods: {
    /**
     * 获取人员列表所有数据
     */
    async getTableData() {
      const typeOpt = {
        '0': '劳务人员',
        '1': '管理人员',
        '2': '特种人员',
        '3': '五大员备案'
      };
      const params = {
        project_id: this.projectId,
        classification: typeOpt[this.tabType],
        page: String(this.pageNo),
        limit: String(this.pageSize)
      };
      const hasSearch = this.hasSearch && this.searchValue;
      if (hasSearch) params.key = this.searchValue;
      this.tableLoading = true;
      const { success, data } = hasSearch
        ? await this.$Model.getWorkersInfoBySearch(params)
        : await this.$Model.getAllWorkersInfo(params);
      this.tableLoading = false;
      if (success && data.data) {
        const { count, workers_info = [] } = data.data;
        this.total = count;
        this.tableData = workers_info;
      }
    },
    /**
     * 设置年龄
     * @param birthday {String} 出⽣⽇期
     */
    setAge(birthday) {
      const currentTime = new Date().getTime();
      const birthTime = new Date(birthday).getTime();
      const year = (currentTime - birthTime) / 365 / 24 / 3600000;
      return Math.floor(year);
    },
    /**
     * pageSize 改变时会触发
     * @param pageSize 每页条数
     */
    handleSizeChange(pageSize) {
      this.pageNo = 1;
      this.pageSize = pageSize;
      this.getTableData();
    },
    /**
     * currentPage 改变时会触发
     * @param pageNo 当前页
     */
    handleCurrentChange(pageNo) {
      this.pageNo = pageNo;
      this.getTableData();
    },
    clear() {
      this.pageNo = 1;
      this.pageSize = 10;
      this.tableLoading = false;
      this.tableData = [];
    },
    /**
     * 人员信息切换
     * @param type {String} 切换类型
     */
    handleTabClick(type) {
      this.tabType = type;
      this.clear();
      this.getTableData();
    },
    /**
     * 人员信息搜索
     */
    handleSearch() {
      this.hasSearch = true;
      this.clear();
      this.getTableData();
    },
    handleRefresh() {
      this.clear();
      this.hasSearch = false;
      this.searchValue = '';
      this.getTableData();
    },
    /**
     * 新增人员
     */
    handleAdd() {
      this.type = 'add';
      this.visible = true;
    },
    /**
     * 查看人员信息
     * @param data {Object}
     */
    handleLook(data) {
      this.type = 'edit';
      this.formData = data;
      this.visible = true;
    },
  }
};
</script>

<style scoped lang="scss">
  @import '../../../assets/styles/table';

  .real-name-page {
    width: 100%;
    height: 100%;
    background: $mainBg;
    padding-top: 30px;
    color: #fff;
    position: relative;
    .breadcrumb {
      position: absolute;
      width: 100%;
      height: 30px;
      top: 0;
      left: 0;
      /deep/ .breadcrumb-comp {
        height: 30px;
      }
    }
    main {
      width: 100%;
      height: 100%;
      background: $baseBg;
      padding: 20px;
      .table-container {
        width: 100%;
        height: 100%;
        background-color: $mainBg;
        border-radius: 10px;
        overflow: hidden;
        position: relative;
        .tabs {
          width: 100%;
          height: 80px;
          display: flex;
          align-items: center;
          padding: 0 10px;
          .list {
            width: 400px;
            height: 100%;
            display: flex;
            align-items: center;
            color: #fff;
            font-size: 14px;
            li {
              width: 80px;
              height: 100%;
              cursor: pointer;
              border-bottom: 2px solid transparent;
              display: flex;
              justify-content: center;
              align-items: center;
              margin-right: 10px;
              transition: all .5s;
              &.active {
                color: $mainColor;
                border-bottom: 2px solid #fff;
              }
            }
          }
          .search-container {
            height: 100%;
            flex: 1;
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 0 20px;
            color: #fff;
            .search-content {
              display: flex;
              justify-content: space-between;
              align-items: center;
            }
            .search-item {
              width: 350px;
              height: 30px;
              display: flex;
              justify-content: space-between;
              align-items: center;
              margin-right: 20px;
              font-size: 14px;
              label {
                width: 80px;
                color: $mainColor;
                margin-right: 5px;
              }
              /*/deep/ .el-input {*/
              /*  height: 30px;*/
              /*  input {*/
              /*    height: 30px;*/
              /*  }*/
              /*}*/
              .el-input__suffix {
                display: flex;
                align-items: center;
              }
            }
          }
        }
        .table-content {
          height: calc(100% - 120px);
          /deep/ .el-table__body-wrapper {
            overflow-y: auto;
            height: 100%;
            padding-bottom: 50px;
          }
          .info {
            display: flex;
            justify-content: flex-start;
            align-items: center;
            img {
              display: inline-block;
              width: 80px;
              height: 80px;
              margin-right: 15px;
            }
            .detail {
              .name {
                padding: 5px 0;
                font-size: 16px;
                font-weight: bold;
              }
              p span {
                margin-right: 10px;
              }
            }
          }
          .detail-info {
            p {
              padding: 3px 0;
              span {
                color: $mainColor;
              }
            }
          }
          .iconfont {
            cursor: pointer;
          }
        }
      }
    }
  }
</style>
